<template>
  <ContainerGroup>
    <Container>
      <div class="form">
        <p>
          <span class="bed-title">操作时间：</span>
          <span>
            <el-date-picker
              v-model="ruleForm.timeDate"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              size="mini"
            />
          </span>
        </p>
        <p
          style="
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 130px;
          "
        >
          <Button icon="search" @click="handerSearch">查询</Button>
          <Button icon="reset" type="info" @click="handlerReset">重置</Button>
        </p>
      </div>
    </Container>
    <Container height="fill" :scroll-y="true" display="flexY">
      <OperationContainer align="right" style="margin-bottom: 10px">
        <Button icon="export" @click="_DownLoad">导出</Button>
      </OperationContainer>
      <el-table
        v-loading="loading"
        :data="listData"
        :span-method="objectSpanMethod"
        border
        height="100%"
      >
        <el-table-column prop="operName" label="操作人姓名" width="200" />
        <el-table-column prop="deptName" label="科室" />
        <el-table-column prop="num" label="预约次数" />
      </el-table>
    </Container>
  </ContainerGroup>
</template>
<script>
import {
  getWorkSumReportDto,
  getWorkSumReportDtoExcel,
} from "../../utils/apis/registrationApi";
export default {
  name: "PersonalWorkload",
  data() {
    return {
      loading: false,
      spanArr: [],
      position: 0,
      listData: [],
      ruleForm: {
        timeDate: [],
      },
    };
  },
  mounted() {
    this.getTime();
    this.queryData();
  },
  methods: {
    getTime() {
      const nowdays = new Date();
      let year = nowdays.getFullYear();
      let month = nowdays.getMonth();
      if (month === 0) {
        month = 12;
        year = year - 1;
      }
      if (month < 10) {
        month = "0" + month;
      }
      const myDate = new Date(year, month, 0);
      const startDate = year + "-" + month + "-01 00:00:00"; // 上个月第一天
      const endDate = year + "-" + month + "-" + myDate.getDate() + " 23:59:00"; // 上个月最后一天
      this.ruleForm.timeDate = [startDate, endDate];
    },
    // 获取患者列表参数
    getListParams() {
      const params = {
        startTime:
          this.ruleForm.timeDate === "" || this.ruleForm.timeDate === null
            ? ""
            : this.ruleForm.timeDate[0], // 预约时间开始
        endTime:
          this.ruleForm.timeDate === "" || this.ruleForm.timeDate === null
            ? ""
            : this.ruleForm.timeDate[1], // 预约时间结束
      };
      return params;
    },
    queryData() {
      // 查询操作
      const params = this.getListParams();
      this.loading = true;
      getWorkSumReportDto(params).then((res) => {
        this.loading = false;
        if (res.data.code === 200) {
          this.listData = res.data.data;
          this.rowspan();
        }
      });
    },
    rowspan() {
      this.spanArr = [];
      this.listData.forEach((item, index) => {
        if (index === 0) {
          this.spanArr.push(1);
          this.position = 0;
        } else {
          if (this.listData[index].operNo === this.listData[index - 1].operNo) {
            this.spanArr[this.position] += 1;
            this.spanArr.push(0);
          } else {
            this.spanArr.push(1);
            this.position = index;
          }
        }
      });
      console.log(this.spanArr);
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      // 表格合并行
      if (columnIndex === 0) {
        const _row = this.spanArr[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col,
        };
      }
    },
    // 重置
    handlerReset() {
      this.getTime();
    },
    handerSearch() {
      this.queryData();
    },
    _DownLoad() {
      const params = this.getListParams();
      getWorkSumReportDtoExcel(params).then((res) => {
        // if (res.data.code === 200) {
        const blob = new Blob([res.data]);
        const fileName = "数据报表个人工作量统计" + ".xlsx";
        const URL = window.URL || window.webkitURL || window.moxURL;
        if ("download" in document.createElement("a")) {
          // 非IE下载
          const elink = document.createElement("a");
          elink.download = fileName; // 命名下载名称
          elink.style.display = "none";
          elink.href = URL.createObjectURL(blob); // 表示一个指定的file对象或Blob对象
          document.body.appendChild(elink);
          elink.click(); // 点击触发下载
          URL.revokeObjectURL(elink.href); // 释放URL 对象
          document.body.removeChild(elink);
        } else {
          // IE10+下载
          navigator.msSaveBlob(blob, fileName);
        }
        // }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.form {
  display: flex;
  align-items: center;
  p {
    margin-right: 40px;
    color: #909399;
  }
}
</style>
